package com.blog.controller.active;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.blog.common.base.BaseController;
import com.blog.common.base.ResponseReslut;
import com.blog.common.util.LayuiPageUtils;
import com.blog.common.util.PageVo;
import com.blog.entity.active.Active;
import com.blog.entity.sys.User;
import com.blog.ro.ActiveRo;
import com.blog.ro.UpdateActiveRo;
import com.blog.service.active.ActiveService;
import com.blog.service.active.ActiveTypeService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/active")
public class ActiveController extends BaseController {

	@Autowired
	private ActiveService activeService;
	
	@Autowired
	private ActiveTypeService activeTypeService;

	@ResponseBody
	@RequestMapping("/save")
	public ResponseReslut save(ActiveRo ro,HttpServletRequest request,HttpSession session)
	{
		User getUser=(User) session.getAttribute("currentUser");
		String mdText= request.getParameter("my-editormd-markdown-doc");
		//String mdTextString= request.getParameter("my-editormd-html-code");
		
		Map<String, Object> mapTag=ro.getTag();
		StringBuffer sb=new StringBuffer();
		if(mapTag!=null)
		{

			for (Entry<String, Object> entry : mapTag.entrySet()) {
				//Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
				//entry.getKey() ;entry.getValue(); entry.setValue();
				//map.entrySet()  返回此映射中包含的映射关系的 Set视图。
				//	            System.out.println("key= " + entry.getKey() + " and value= "
				//	                      + entry.getValue());
				sb.append(","+entry.getValue());  //标签 问题  ，1,2，3   最后一个是没有，号的 所有 后面加，
			}
			sb.append(",");
		}
		if(StringUtils.isEmpty(ro.getPic())){
			ro.setPic("chb.jpg");//添加默认的 图片 
		}
		Active active=new Active();
		active.setUserId(getUser.getId());
		active.setType(ro.getType());
		active.setText(mdText);
		active.setTitle(ro.getTitle());
		active.setTag(sb.toString());
		active.setVisits(100);
		active.setState(1);
		active.setPic(ro.getPic());
		active.setCreateTime(new Date());
		active.setUpdateTime(new Date());
		if(activeService.saveActive(active)>0)
		{
			return setResultSuccess("发布成功！");
		}
		return resultError("发布失败！");
	}


	/**
	 * 
	 * @author: chb
	 * @date: 2018年7月27日 上午9:24:00
	 * @param parmsMap
	 * @return  
	 * 作用：  查询所有文章 
	 */
	@ResponseBody
	@RequestMapping("/list") 
	public PageVo listAll(@RequestParam Map<String,Object> parmsMap)
	{
		Map<String,Object> pageMap=LayuiPageUtils.PageMap(parmsMap);
		List<Active> listActive=activeService.listAllActive(pageMap);
		Integer count=activeService.countActive(pageMap);
		//这里可以 你可以链表查询  也可以分开查询 
		//set集合可以高效除去重复
//		Set  set=new HashSet();
//		for(Active ac : listActive)
//		{
//			set.add(ac.getActiveId());
//		}
	
		PageVo  pageVo=new PageVo(0,"查询成功",count,listActive);
		//layui  code返回必须是0  
		return pageVo;
	}


	@RequestMapping("/showMdText/{activeId}") 
	public ModelAndView showMdText(@PathVariable("activeId") Integer activeId)
	{
		Map<String, Object> map=new HashMap<>();
		Active active=activeService.selectShowMdText(activeId);
		map.put("mdText", active.getText());
		return view("/active/showActive",map);
	}

	//跳转 页面 
	@RequestMapping("/updateActive/{activeId}") 
	public ModelAndView updateActive(@PathVariable("activeId") Integer activeId)
	{
		Map<String, Object> map=new HashMap<>();
		Active active=activeService.selectShowMdText(activeId);
		map.put("active", active);
		return view("/active/updateActive",map);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public ResponseReslut update(UpdateActiveRo activeRo,HttpServletRequest request)
	{
		String mdText= request.getParameter("my-editormd-markdown-doc");
		//String mdTextString= request.getParameter("my-editormd-html-code");
		Active active=new Active();
		BeanUtils.copyProperties(activeRo,active);	
		active.setUpdateTime(new Date());
		Map<String, Object> mapTag=activeRo.getTag();
		StringBuffer sb=new StringBuffer();
		if(mapTag!=null)
		{
			for (Entry<String, Object> entry : mapTag.entrySet()) {
				 		sb.append(","+entry.getValue());   
			}
			sb.append(",");
		}
		active.setTag(sb.toString());
		active.setText(mdText);
		Integer  num=activeService.updateActive(active);
		if(num>0)
		{
			return setResultSuccess("修改成功！");
		}
		return resultError("修改失败！");
	}
	
	@ResponseBody
	@GetMapping("/updateState/{activeId}")
	public ResponseReslut updateState(@PathVariable("activeId") Integer activeId)
	{
		Active active=new Active();
		active.setActiveId(activeId);
		active.setState(0);
		active.setUpdateTime(new Date());
		Integer  num=activeService.updateActive(active);
		if(num>0)
		{
			return setResultSuccess("修改成功！");
		}
		return resultError("修改失败！");
	}
	
	

}
